package com.leapp.sk.inter

import android.content.Context
import android.os.Handler
import android.os.Message
import com.tencent.tauth.IUiListener
import com.tencent.tauth.UiError

/**
 * 分享
 * @author  chaohaipeng  on 2018/8/4.
 */
class BaseUIListener :IUiListener {
    private var mContext: Context? = null
    private var mScope: String? = null
    private var mIsCaneled: Boolean = false
    private val ON_COMPLETE = 0
    private val ON_ERROR = 1
    private val ON_CANCEL = 2

    constructor(mContext: Context) {
        this.mContext = mContext
    }
    constructor(mContext: Context, mScope: String) {
        this.mContext = mContext
        this.mScope = mScope
    }

    private val mHandler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                ON_COMPLETE -> {
//                    val response = msg.obj as JSONObject
//                    Util.showResultDialog(mContext, response.toString(), "onComplete")
//                    Util.dismissDialog()
                }
                ON_ERROR -> {
                    val e = msg.obj as UiError
//                    Util.showResultDialog(mContext, "errorMsg:" + e.errorMessage
//                            + "errorDetail:" + e.errorDetail, "onError")
//                    Util.dismissDialog()
                }
//                ON_CANCEL -> Util.toastMessage(mContext as Activity, "onCancel")
            }
        }
    }
    fun cancel() {
        mIsCaneled = true
    }

    override fun onComplete(p0: Any?) {
        if (mIsCaneled) return
        val msg = mHandler.obtainMessage()
        msg.what = ON_COMPLETE
        msg.obj = p0
        mHandler.sendMessage(msg)
    }

    override fun onCancel() {
        if (mIsCaneled) return
        val msg = mHandler.obtainMessage()
        msg.what = ON_CANCEL
        mHandler.sendMessage(msg)
    }

    override fun onError(p0: UiError?) {
        if (mIsCaneled) return
        val msg = mHandler.obtainMessage()
        msg.what = ON_ERROR
        msg.obj = p0
        mHandler.sendMessage(msg)
    }
}